﻿using System;
using System.Linq;
namespace Portal.Data.Crawler
{
    public class CommonCrawler
    {
        private readonly HtmlParser _parser;

        public CommonCrawler()
        {
            _parser = new HtmlParser();
        }

        /// <summary>
        /// Lay noi dung Truyen tranh theo domain duoc xac dinh san
        /// </summary>
        public string GetChapBody(string crawlUrl, out string errorMessage)
        {
            try
            {
                string openDetail = "";
                string closeDetail = "";
                var urlLower = crawlUrl.ToLower();
                if (urlLower.IndexOf("vitaku.com") >= 0)
                {
                    openDetail = "<div class=\"page";
                    closeDetail = "</div>";
                }
                else if (urlLower.IndexOf("comicvn.net") >= 0)
                {
                    openDetail = "<textarea id=\"txtarea";
                    closeDetail = "</textarea>";
                }
                else if (urlLower.IndexOf("blogtruyen.com") >= 0)
                {
                    openDetail = "<article id=\"content";
                    closeDetail = "</article>";
                }
                else if (urlLower.IndexOf("manga24h.com") >= 0)
                {
                    openDetail = "data ='";
                    closeDetail = "';";
                }
                else if (urlLower.IndexOf("truyentranh8.com") >= 0)
                {
                    //danh sach link image
                    openDetail = "lstImages.push(\"";
                    closeDetail = "\");";
                }
                else
                {
                    errorMessage = crawlUrl + ": chua dinh dang content format!";
                    return "";
                }

                errorMessage = "";

                var sources = Common.GetHtmlSources(crawlUrl);
                if (sources.Length > 0)
                {
                    sources = _parser.Replace(sources, HtmlParser.Patterns.BREAK, " ");
                    sources = _parser.Replace(sources, HtmlParser.Patterns.MultiSpacePattern, HtmlParser.Patterns.SINGLE_SPACE);
                    var ss = _parser.GetContentInNode(sources, openDetail, closeDetail);
                    if (ss.Count > 0)
                    {
                        if (urlLower.IndexOf("truyentranh8.com") >= 0)
                        {
                            var newBody = ss.Select(link => link.Substring(openDetail.Length, link.Length - (closeDetail.Length + openDetail.Length))).Aggregate("", (current, realLink) => current + string.Format("<img src=\"{0}\" alt=''/>", realLink));
                            return newBody;
                        }

                        var body = ss[0];
                        if (urlLower.IndexOf("manga24h.com") >= 0)
                        {
                            //remove node bound with special
                            var idx = body.IndexOf("'");
                            body = body.Substring(idx + 1, body.Length - 1 - (closeDetail.Length + idx));
                            var imgUrls = body.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                            var newBody = imgUrls.Aggregate("", (current, imgUrl) => current + string.Format("<img src=\"{0}\" alt=''/>", imgUrl));
                            return newBody;
                        }

                        else
                        {
                            //remove node bound
                            var idx = body.IndexOf(">");
                            body = body.Substring(idx + 1, body.Length - 1 - (closeDetail.Length + idx));
                            if (urlLower.IndexOf("vitaku.com") >= 0)
                            {
                                body = _parser.RemoveTagAndContentInTags(body, new[] { "div" }); //remove all div+content
                            }
                            else if (urlLower.IndexOf("comicvn.net") >= 0)
                            {
                                body = _parser.RemoveTagOnly(body, new[] { "div", "span", "textarea" });
                            }
                            else if (urlLower.IndexOf("blogtruyen.com") >= 0)
                            {
                                body = _parser.RemoveTagOnly(body, new[] { "div", "article" });
                            }
                            return _parser.RemoveProperties(body, new[] { "width", "height" });
                        }
                    }
                }
                else
                {
                    errorMessage = crawlUrl + ": null data";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return "";
        }
    }
}
